﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SponsorshipManager.Database;

namespace SponsorshipManager.BusinessLayer
{
    public class Relationship : IDbBasedObject
    {
        public int RelationshipId { get; set; }
        public enum StateEnum{NotStarted, Active, Terminated}
        public Child Child { get; set; }
        private int childId { get; set; }
        public SponsorDTO Sponsor { get; set; }
        private int sponsorId { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public StateEnum State { 
            get 
            {
                bool startSet = !(StartDate==DateTime.MinValue || StartDate==null);
                bool endSet = !(EndDate==DateTime.MinValue || EndDate==null);

                if (endSet)
                {
                    // end set (irrelevant if start set or not)
                    return StateEnum.Terminated;
                }
                else
                {
                    // if start set then in progress, otherwise never startd.
                    if(startSet)
                    {
                        return StateEnum.Active;
                    }
                    else
                    {
                        return StateEnum.NotStarted;
                    }
                }
            } 
        }

        public List<NoteDTO> Notes { get 
        {
            return NoteDTO.Notes("sponsors", RelationshipId);
        } 
        }
        public List<Quota> Quotas { get; private set; }
        public List<Corrispondence> Corrispondences { get; private set; }

        public bool NeedsToSave { get ; set ; }

        public bool Load()
        {
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name = "uspGetPerson";
            sp.AddParameter("PersonId", DbType.Int32, RelationshipId);
            sp.Token = User.CurrentUser.Token;
            int ret = sp.Execute();
            if (ret == 1) LoadDataRow(sp.QueryResult.Rows[0]);
            return ret == 1;
        }

        public bool Save()
        {
            throw new NotImplementedException();
        }

        internal static List<Relationship> Relationships(Child c)
        {
            List<Relationship> relationships = new List<Relationship>();

            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name="uspGetChildRelationships";
            sp.AddParameter("ChildId",DbType.Int32,  c.ChildId);
            sp.Token = User.CurrentUser.Token;
            int ret = sp.Execute();
            if (ret > 0)
            {
                foreach (DataRow dr in sp.QueryResult.Rows)
                {
                    Relationship r = new Relationship();
                    r.LoadDataRow(dr);
                    relationships.Add(r);
                }
            }
            return relationships;
        }

        internal static List<Relationship> Relationships(SponsorDTO s)
        {
            List<Relationship> ret = new List<Relationship>();

            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name = "uspGetSponsorRelationships";
            sp.AddParameter("SponsorId", DbType.Int32, s.SponsorId);
            sp.Token = User.CurrentUser.Token;
            sp.Execute();

            foreach (DataRow dr in sp.QueryResult.Rows) 
            {
                Relationship r = new Relationship();
                r.LoadDataRow(dr);
            }
            return ret;
        }

        private bool LoadDataRow(DataRow dr)
        {
            childId = (int)dr["childId"];
            sponsorId = (int)dr["sponsorId"];
            StartDate = (DateTime)dr["startDate"];
            EndDate = (DateTime)dr["EndDate"];
            return true;
        }

    }
}
